package lumis.service.doui.propertybag;

import java.util.List;
import java.util.Map;

import org.w3c.dom.Node;

import lumis.doui.source.IDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.table.TableSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.propertybag.Property;
import lumis.portal.propertybag.PropertyBag;
import lumis.util.ITransaction;
import lumis.util.XmlUtil;

/**
 * Provides the local properties of a property bag.
 * <p>
 * Fills the fields id (string), name (string) and values (string). The values are separated by a '\n'.
 * <p>
 * Must contains the elements:
 * <ul>
 * 	<li><code>propertyBagSourceId</code>: The identifier of the source that contains the propertyBagId value.</li>
 * 	<li><code>propertyBagFieldId</code>: The identifier of the field (in the source specified above) that contains the propertyBagId value.</li>
 * </ul>
 * 
 * @version $Revision: 10825 $ $Date: 2009-08-04 12:36:40 -0300 (Tue, 04 Aug 2009) $
 * @since 5.0.0
 */
public class LocalPropertiesDataProvider implements IDataProvider<TableSource>
{
	public void loadData(SessionConfig sessionConfig, TableSource source, ITransaction transaction) throws PortalException 
	{
		PropertyBag propertyBag = getPropertyBag(source);
		if(propertyBag != null)
		{
			TabularData data = source.getData();
			Map<String, Property> localPropertyMap = propertyBag.getLocalPropertyMap();
			for (Map.Entry<String, Property> entry: localPropertyMap.entrySet())
			{
				Property property = entry.getValue();
				if(property.isPrivate())
					continue;
				ISourceData newRow = data.addRow();
				newRow.put("id", property.getName());
				newRow.put("name", property.getName());

				StringBuffer stringValues = new StringBuffer();
				List<String> values = property.getValues();
				for(String value:values)
				{
					stringValues.append(value);
					stringValues.append("\n");
				}
				newRow.put("values", stringValues.toString());
			}
			data.sort(source);
		}
	}

	/**
	 * Returns the property bag for an item being added or edited.
	 * @param source the source of the item being added or edited.
	 * @return the property bag, or <code>null</code> if it does not exist.
	 * @since 5.0.0
	 */
	private PropertyBag getPropertyBag(TableSource source) throws PortalException
	{
		// read the property bag identifier from the source that contains it
		Node sourceNode = source.getDefinitionNode();
		String propertyBagSourceId = XmlUtil.readNodeString("propertyBagSourceId", sourceNode);
		String propertyBagFieldId = XmlUtil.readNodeString("propertyBagFieldId", sourceNode);
		Source<?> propertyBagSource = source.getSourceContainer().getSourceById(propertyBagSourceId);
		String propertyBagId = propertyBagSource.getData().get(propertyBagFieldId, String.class);
		
		// get the property bag
		PropertyBag propertyBag;
		if (propertyBagId != null)
			propertyBag = ManagerFactory.getPropertyBagManager().get(propertyBagId);
		else
			propertyBag = null;
		
		return propertyBag;
	}
}
